package com.stx.mall.web;

import com.stx.mall.model.User;
import com.stx.mall.service.UserService;
import com.stx.mall.vo.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("index.do")
    public String index(){
        return "login";
    }

    @RequestMapping("list.do")
    public String list(Model model){
        //查询所有用户信息
        List<User> userList = userService.findAllUsers();
        model.addAttribute("userList", userList);
        return "admin/console";
    }

    @RequestMapping(value = "reg.do", method = RequestMethod.POST)
    public String reg(User user, Model model){
        userService.register(user);
        return "redirect:list.do";
    }

    @RequestMapping(value = "checku.do", method = RequestMethod.GET)
    @ResponseBody
    public AjaxResult check(String uname){
        boolean exists = userService.isExist(uname);
        if(exists){
            //当前用户名已被占用
            return new AjaxResult(false, "当前用户名已被注册！");
        }
        return new AjaxResult(true, "");
    }

}
